﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PresentationControls;

namespace Hazop
{
    public partial class AddNodeForm : Form{
        public delegate void treeViewAddNodeEventHandler(string name, string pidList);
        public event treeViewAddNodeEventHandler treeViewAddNode = null;

        public AddNodeForm()
        {
            InitializeComponent();
            LoadData();
        }

        private void LoadData(){
            foreach (string s in Program.Workspace.GetPidNumList()){
                cmbPidList.Items.Add(s);
            }
            
        }

        private void btnCommit_Click(object sender, EventArgs e){
            if (tbxNodeName.Text == ""){
                MessageBox.Show("请填写节点名字");
                return;
            }
            if(Program.Workspace.HasNode(tbxNodeName.Text)){
                MessageBox.Show("节点已经存在!");
                return; 
            }
               
            List<string> list = new List<string>();
            foreach (CheckBoxComboBoxItem item in cmbPidList.CheckBoxItems){
                if (item.Checked){
                    list.Add(item.Text);
                }
            }
            string pidName = string.Empty;
            if (list.Count > 0) {
                foreach (string s in list)
                {
                    pidName += s + ",";
                }
                pidName = pidName.Remove(pidName.Length - 1);
            }
            OnTreeViewNodeAdded(tbxNodeName.Text,pidName);
            this.Close();
        }
        
        public void OnTreeViewNodeAdded(string s, string pidName){
            if (treeViewAddNode != null)
                treeViewAddNode(s, pidName);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
